#include <sys/time.h>
#include <sys/select.h>

struct lab5_hdr {
  uint16_t packet_number;
  uint16_t length;
  uint32_t timestamp;
  uint32_t sequence_number;
  uint32_t ack_flag;
  uint16_t fin_flag;
};


#define MAX_PACKET 1400
#define MAX_SEGMENT (MAX_PACKET-sizeof(struct lab5_hdr))

int my_socket(int domain, int type, int protocol);
int my_connect(int socket,struct sockaddr_in *toaddr,int addrsize);
void my_sendf(int sock, void * buf, size_t len, int fin, int tries);
void my_send(int sock, void *buf, size_t len);

int my_recv(int sock, void * buffer, size_t length);
int my_close(int sock);
int my_rtt(int sock);

int timeval_to_msec(struct timeval *t);
void msec_to_timeval(int millis, struct timeval *out_timeval);
int current_msec();

void sendAck( int sock, int ackNum, char * packet );
void update_rtt (int rtt, int timeout );

int receiveAndInterpret( int sock, char * receive, int * finf, int * ackn, int * timeDiff, int * nextExpect );
int setupPacket ( char * packet, void * buf, int len, int numRemaining, int fin, int retransmitted );
